import {  getUserInfo } from "../utils/auth.js";


const baseUrl = 'https://api.coze.cn'
const token = "Bearer pat_xUh4FWdh4d2nq0c79Smoy0H7myrCqjaVK5tvr8h3k1Xm8yKpZiAfcaa5cJZOsgBM"
const botId = "7563219453002137600"

// 创建会话
export const createSession = () => {
  return new Promise((resolve) => {
    wx.request({
      url: `${baseUrl}/v1/conversation/create`,
      method:'POST',
      header: {
        "Authorization": token,
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        bot_id: botId
      }),
      success:(res)=>{
        resolve(res.data)
      }
    })
  })
}
// 删除会话
export const deleteSession = (conversation_id) => {
  return new Promise((resolve) => {
    wx.request({
      url: `${baseUrl}/v1/conversations/${conversation_id}`,
      method:'DELETE',
      header: {
        "Authorization": token,
        "Content-Type": "application/json"
      },
      success:(res)=>{
        resolve(res.data)
      }
    })
  })
}

//查看会话消息
export const retrieveConv = (conversation_id) => {
  return new Promise((resolve) => {
    wx.request({
      url: `${baseUrl}/v1/conversation/retrieve`,
      method:'GET',
      header: {
        "Authorization": token,
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        conversation_id: conversation_id
      }),
      success:(res)=>{
        resolve(res.data)
      }
    })
  })
}

// 创建消息
export const createMsg = (conversation_id, question) => {
  return new Promise((resolve) => {
    wx.request({
      url: `${baseUrl}/v1/conversation/message/create?conversation_id=${conversation_id}`,
      method:'POST',
      header: {
        "Authorization": token,
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        role: "user",
        content: question,
        content_type: "text"
      }),
      success:(res)=>{
        resolve(res.data)
      }
    })
  })

}


// 发起对话
export const chatWithBot = (conversation_id, additional_messages) => {
  console.log("add", additional_messages)
  return wx.request({
      url: `${baseUrl}/v3/chat?conversation_id=${conversation_id}`,
      method:'POST',
      enableChunked: true, // 开启流式传输
      responseType: "arraybuffer",
      header: {
        "Authorization": token,
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        bot_id: botId,
        user_id: getUserInfo().id,
        stream: true,
        additional_messages: additional_messages,
      })
  })
}




